using System;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class TestWebviewUI : MonoBehaviour
{
    public Button createBtn;

    public Button removeBtn;

    public Button loadurlBtn;

    public Button loadhtmlstringBtn;

    public Button loadDataBtn;

    public Button evaluatejsBtn;

    public Button setvisibilityBtn;

    public Button setmarginsBtn;

    public Button reloadBtn;

    public Button stoploaddingBtn;

    public Button goforwardBtn;

    public Button gobackBtn;

    public Button loadUrlLocalBtn;

    private OpenHarmonyWebview _openHarmonyWebview;
    
    private bool isVisible = true;

    
    // Start is called before the first frame update
    void Start()
    {
#if UNITY_OPENHARMONY
        _openHarmonyWebview = new OpenHarmonyWebview();
        // 创建 webview，成功后加载网页
        createBtn.onClick.AddListener(() => _openHarmonyWebview.CreateWebview(Screen.width / 8, Screen.height / 8, Screen.width / 8, Screen.height / 2, true, () =>
        {
            _openHarmonyWebview.LoadURL("https://baidu.com/");
        }));
        // 移除 webview
        removeBtn.onClick.AddListener(_openHarmonyWebview.RemoveWebview);
        // 载入新的 url
        loadurlBtn.onClick.AddListener(() => _openHarmonyWebview.LoadURL("https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/application-dev-guide"));
        // 载入 html 数据
        loadhtmlstringBtn.onClick.AddListener(() =>
            _openHarmonyWebview.LoadHTMLString("<html><body bgcolor=\"white\">LoadHTMLString:<pre>loadHTMLString</pre></body></html>",
                ""));

        loadDataBtn.onClick.AddListener(() =>
        {
            string html = @"<!doctype html><html><body>
          <h3>Base64 LoadData</h3>
        </body></html>";
            string b64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(html));
            // 如果不加密 encoding 则为 utf-8 
            _openHarmonyWebview.LoadData(b64, "text/html", "base64", "");
        });
        // 加载本地网页
        loadUrlLocalBtn.onClick.AddListener(() =>
            _openHarmonyWebview.LoadURL("resource://rawfile/Data/StreamingAssets/index.html"));
        // 执行 webview 中的 js 方法
        evaluatejsBtn.onClick.AddListener(() => _openHarmonyWebview.EvaluateJS("test()"));
        // 设置webview是否可见
        setvisibilityBtn.onClick.AddListener(() =>
            {
                isVisible = !isVisible;
                _openHarmonyWebview.SetVisibility(isVisible);
            });
        // webview 设置边缘，四个取值为左、上、右、下
        setmarginsBtn.onClick.AddListener(() =>
            _openHarmonyWebview.SetMargins(0, 0, 0, Screen.height / 2));
        reloadBtn.onClick.AddListener(_openHarmonyWebview.Reload);
        stoploaddingBtn.onClick.AddListener(_openHarmonyWebview.StopLoading);
        goforwardBtn.onClick.AddListener(_openHarmonyWebview.GoForward);
        gobackBtn.onClick.AddListener(_openHarmonyWebview.GoBack);
#endif
       }


    
    // Update is called once per frame
    void Update()
    {
        
    }
}
